/*
 * @Author: your name
 * @Date: 2022-03-21 16:05:16
 * @LastEditTime: 2023-05-16 17:37:49
 * @LastEditors: YNT 2501512800@qq.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \22-03-19 智慧社区\pages\index\components\home\index.js
 */
// pages/index/components/home/index.js
import globalModel from '../../../../api/global'
import Message from '../../../../api/message'
import SpecialModel from '../../../../api/special'
import dayjs from '../../../../utils/dayjs'
import SCORE from '../../../../api/score'
import { setStorage } from '../../../../utils/storageSyncTool'
Component({
  options: {
    styleIsolation: 'shared',
  },
  properties: {},
  data: {
    nvabarData: {
      navigationBarTextStyle: 'white', // 胶囊主题 white || black
      navigationBarBackground: '#349CD1', // 导航栏背景色
      navigationBarTitleText: '洋安E未来',
      hideCapsule: true,
    },
    userInfo: {},
    isLogin: false,
    isSign: false,
    myScore: 0,
    newsList: [],
    passStatus: '',
    swiperList: '',
  },
  observers: {
    userInfo(userInfo) {
      let { avatar_url, nick_name } = userInfo || {}
      let isLogin = Boolean(avatar_url && nick_name)
      this.setData({ isLogin })
    },
  },
  lifetimes: {
    attached: function () {
      globalModel
        .getUserinfo()
        .then(({ data }) => {
          let status = parseInt(data.status) //'审核状态 0 未填写 1 待审核 2 审核通过 3 审核不通过'
          setStorage('status', status)
          setStorage('userInfo', data)
          this.setData({
            userInfo: data,
            passStatus: status,
          })
          if (status == 2) {
            wx.showLoading()
            let p1 = Message.messageList({ per_page: 2, page: 1 })
              .then(({ data }) => data)
              .catch((err) => [])

            let p2 = SpecialModel.newsList({ page: 1, per_page: 10 })
              .then(({ data }) => data)
              .catch((err) => [])

            // let p3 = Message.release({ page_size: 1 })
            //   .then(({ data }) => data)
            //   .catch((err) => [])

            let p4 = SpecialModel.getSignStatus()
              .then(({ data }) => data)
              .catch((err) => [])

            let p5 = SCORE.checkScore()
              .then(({ data }) => data)
              .catch((err) => {})

            let p6 = SpecialModel.bannerList(1)
              .then(({ data }) => data)
              .catch((err) => [])

            Promise.all([p1, p2, p4, p5, p6]).then(
              ([message, consults, signStatus, score, banners]) => {
                // wx.hideLoading()
                if (message) {
                  message.forEach(function (item) {
                    item.create_time = dayjs(item.create_time).format(
                      'YYYY-MM-DD HH:mm'
                    )
                  })
                }
                let newsList = consults.items
                this.setData({
                  myScore: score.balance,
                  isSign: signStatus.is_sign,
                  newsList: newsList,
                  // release: release,
                  messageList: message,
                  swiperList: banners,
                })
              }
            )
          } else {
            let p1 = SpecialModel.bannerList()
              .then(({ data }) => data)
              .catch((err) => [])
            let p2 = SpecialModel.newsList({ page: 1, per_page: 10 })
              .then(({ data }) => data)
              .catch((err) => [])
            Promise.all([p1, p2]).then(([banners, consults]) => {
              let newsList = consults.items
              this.setData({
                newsList: newsList,
                swiperList: banners,
              })
            })
          }
        })
        .catch((err) => [])
        .finally(() => {
          wx.hideLoading()
        })
    },
  },

  pageLifetimes: {
    show: function () {
      if (this.data.passStatus == 2) {
        Message.messageList({ page_size: 2 })
          .then(({ data }) => {
            data.forEach(function (item) {
              item.create_time = dayjs(item.create_time).format(
                'YYYY-MM-DD HH:mm'
              )
            })
            this.setData({
              messageList: data,
            })
          })
          .catch((err) => [])
      }
    },
  },

  methods: {
    toPage(e) {
      let { url } = e.currentTarget.dataset
      url && wx.navigateTo({ url })
    },
    toLogin() {
      wx.redirectTo({ url: '/pages/login/login' })
    },
    checkAll() {
      wx.navigateTo({ url: '/pages/info/index' })
    },
    realName() {
      wx.navigateTo({
        url: '/subPackages/otherPackages/pages/realnameregistration/index',
      })
    },
    checkNotice() {
      wx.navigateTo({
        url: '/pages/notice/index',
      })
    },
    sign() {
      let that = this
      SpecialModel.sign()
        .then((res) => {
          SCORE.checkScore()
            .then((res) => {
              that.setData({
                myScore: res.data.balance,
                isSign: true,
              })
            })
            .catch((err) => {})
          let score = res.score
          this.triggerEvent('popup', score)
        })
        .catch((err) => {
          this.triggerEvent('hasSigned')
        })
    },

    // 跳转链接
    linkTo(e) {
      let linkTo = e.currentTarget.dataset.link
      if (linkTo) {
        wx.navigateTo({
          url: linkTo,
        })
      }
    },
  },
})
